package net.mihome.app.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;

import net.mihome.app.AppContext;
import net.mihome.app.R;
import net.mihome.app.adapter.RssResourceAdapter;
import net.mihome.app.bean.RssResource;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RssResourceActivity extends Dialog{
	private RssResourceAdapter resourceAdapter;
	private ListView rssResourceListView;
	private List<RssResource> rssResourceList;
	private Button okButton;
	private Button cancleButton;
	private List<String> listChecked=new ArrayList<String>();
	private AppContext appContext;// 全局Context
	public RssResourceActivity(Context context, boolean cancelable,
			OnCancelListener cancelListener,List<String> listCheced) {
		super(context, cancelable, cancelListener);
		this.listChecked=listCheced;
		// TODO Auto-generated constructor stub
	}

    public RssResourceActivity(Context context) {
        super(context);
    }

    public RssResourceActivity(Context context, int theme) {
        super(context, theme);
    }

    @Override
	protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
        //去除title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //去掉Activity上面的状态栏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.rss_add);
        appContext = (AppContext)getContext().getApplicationContext();
		initView();
        new AsyncTask<String, Integer, List<String>>() {
            @Override
            protected List<String> doInBackground(String... strings) {
                String rssCheckedString=appContext.getUserPropertie("rss_checked");
                List<String> list=new ArrayList<String>();
                if(rssCheckedString!=null){
                    list=RssResourceActivity.string2list(rssCheckedString);
                }
                return list;
            }

            @Override
            protected void onPostExecute(List<String> strings) {
                listChecked=strings;
                initRssSourceListView();
                super.onPostExecute(strings);
            }
        }.execute();
	}
	/**
	 * 初始化界面
	 * */
	public void initView(){
		okButton=(Button) findViewById(R.id.dingyue_add_queding);
		okButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				List<String> list=resourceAdapter.checkdList;
//				appContext.setProperty("rss_checked",list2strString(list));
				appContext.setUserPropertie("rss_checked",list2strString(list));
				RssResourceActivity.this.cancel();
			}
		});
		cancleButton=(Button) findViewById(R.id.dingyue_add_cancle);
		cancleButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				cancel();
			}
		});
	}
	/**
	 * 初始化rss订阅源列表
	 * */
	public void initRssSourceListView(){
		this.rssResourceListView=(ListView) findViewById(R.id.rss_add_resource_lv);
		String[] listOfRss=this.getContext().getResources().getStringArray(R.array.rss_resource_list);
		this.rssResourceList=RssResource.parseList(listOfRss);
		resourceAdapter=new RssResourceAdapter(this.getContext(),this.rssResourceList,this.listChecked);
		this.rssResourceListView.setAdapter(resourceAdapter);
		this.resourceAdapter.notifyDataSetChanged();
	}
	/**
	 * list与字符串之间转换
	 * */
	public static String list2strString(List<String> list){
		StringBuilder resString=new StringBuilder("");
		if(list!=null){
			Iterator<String> iterator=list.iterator();
			while(iterator.hasNext()){
				if(resString.length()>0){
					resString.append("--");
				}
				resString.append(iterator.next());
			}
		}
		return resString.toString();
	}
	/**
	 * list与字符串之间转换
	 * */
	public static List<String> string2list(String strs){
		String[] listStrings=strs.split("--");
		List<String> list=new ArrayList<String>();
		for(String l:listStrings){
			list.add(l);
		}
		return list;
	}
	
}
